package cn.zxh.csdn_maven.base.util.File;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {
	private PropertiesUtil(){}
	public static void main(String[] args) {
		
	}
	public static Object getValueFromPropertiesKey(String properties_path,String key){
		Map<Object, Object> properties = getProperties(properties_path);
		if(properties.containsKey(key)){
			return properties.get(key);
		}
		return null;
	}
	public static Map<Object, Object> getProperties(String properties_path){
		Properties prop = new Properties();
		try {
			// 读取属性文件base.properties
			InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(properties_path);
			/*ClassLoader classLoader = PropertiesUtil.class.getClassLoader();
			URL resource = classLoader.getResource(properties_path);
			String path = resource.getPath();
			InputStream in = new BufferedInputStream(new FileInputStream(path));*/
			prop.load(in); /// 加载属性列表
			/*
			 * Iterator<String> it=prop.stringPropertyNames().iterator();
			 * while(it.hasNext()){ String key=it.next();
			 * System.out.println(key+":"+prop.getProperty(key)); }
			 */
			in.close();

			/// 保存属性到b.properties文件
			/*
			 * FileOutputStream oFile = new FileOutputStream("b.properties",
			 * true);//true表示追加打开 prop.setProperty("phone", "10086");
			 * prop.store(oFile, "The New properties file"); oFile.close();
			 */
		} catch (Exception e) {
			System.out.println(e);
		}
		return prop;
	}
}
